package cn.htu.edu.service.impl;

import cn.htu.edu.common.ResponseHelper;
import cn.htu.edu.common.ResponseModel;
import cn.htu.edu.entity.Orders;
import cn.htu.edu.entity.Person;
import cn.htu.edu.entity.Users;
import cn.htu.edu.entity.baseEntity.Orderz;
import cn.htu.edu.entity.baseEntity.Userz;
import cn.htu.edu.mapper.OrdersMapper;
import cn.htu.edu.mapper.PersonMapper;
import cn.htu.edu.mapper.UsersMapper;
import cn.htu.edu.payload.base.PageRequest;
import cn.htu.edu.payload.request.orders.Order;
import cn.htu.edu.payload.request.orders.OrdersPerson;
import cn.htu.edu.payload.request.person.Personz;
import cn.htu.edu.payload.request.users.User;
import cn.htu.edu.service.IUsersService;
import cn.htu.edu.utils.StringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dakai
 * @since 2024-05-10
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private PersonMapper personMapper;

    @Override
    public List<Users> getAllUsers() {
        List<Users> users = usersMapper.getAllUsers();
        for (Users user : users) {
            List<Orders> orders = usersMapper.getOrdersByUserId(user.getId());
            for (Orders order : orders) {
                List<Person> person = usersMapper.getPersonByOrderId(order.getId());
                order.setPersons(person);
//                order.setPersons(new ArrayList<>());
            }
            user.setOrders(orders);
        }
        return users;
    }

    @Override
    public ResponseModel saveUsers(Userz userz) {
        if("".equals(userz.getId())){
            //从主表users插入一条记录
            userz.setId(StringUtil.createUUID());
            if("".equals(userz.getName())){
                return ResponseHelper.buildResponseModel("name为空，插入失败");
            }
            if("".equals(userz.getEmail())){
                return ResponseHelper.buildResponseModel("email为空，插入失败");
            }
            long countUsers = 0;
            //获取主表id
            String userId = userz.getId();
            //如果orders表插入数据为空，抛出异常
            if(userz.getOrders().size() == 0 || userz.getOrders() == null){
                return ResponseHelper.buildResponseModel("插入失败");
            }
            //for循环遍历orders表
            for (Orderz orderz: userz.getOrders()) {
                orderz.setId(StringUtil.createUUID());
                //把主表id存到orders表中
                orderz.setUid(userId);
                //接收order表中的数据
                if("".equals(orderz.getOname())){
                    return ResponseHelper.buildResponseModel("oname为空，插入失败");
                }
                countUsers = usersMapper.saveOrders(orderz);
                //判断orders表长度，为空抛出异常
                if(countUsers <= 0){
                    return ResponseHelper.buildResponseModel("orderz为空，插入失败");
                }
            }
            long count = usersMapper.saveUsers(userz);
            if(count != 1){
                return ResponseHelper.buildResponseModel("插入失败");
            }
            return ResponseHelper.buildResponseModel("添加成功");
        }else{
            //判断是否存在
            String count = usersMapper.countUserId(userz.getId());
            if ("0".equals(count)){
                return ResponseHelper.buildResponseModel("主键不存在,修改失败");
            }
            if(!new ArrayList<>().equals(userz.getOrders())){
                for (Orderz orderz: userz.getOrders()) {
                    ordersMapper.updateOrdersPerson(orderz);
                }
            }
            usersMapper.updateUsers(userz);
            return ResponseHelper.buildResponseModel("修改成功");
        }
    }

    @Override
    public IPage<Users> getSelectUsers(User request) {
        IPage<Users> usersIPage = usersMapper.getSelectUsers(PageRequest.getPage(request), request.getName(), request.getEmail());
        List<Users> users = usersIPage.getRecords();
        for (Users user:users) {
            List<Orders> orders = usersMapper.getOrdersByUserId(user.getId());
            for (Orders order : orders) {
                List<Person> person = usersMapper.getPersonByOrderId(order.getId());
                order.setPersons(person);
            }
            user.setOrders(orders);
        }
        return usersIPage;
    }

    @Override
    public IPage<Users> getSelectUsers1(User request) {
        IPage<Users> usersIPage = usersMapper.getSelectUsers(PageRequest.getPage(request), request.getName(), request.getEmail());
        List<Users> users = usersIPage.getRecords();
        for (Users user:users) {
            List<Orders> orders = usersMapper.getOrdersByUserId(user.getId());
            for (Orders order : orders) {
                order.setPersons(new ArrayList<>());
            }
            user.setOrders(orders);
        }
        return usersIPage;
    }

    @Override
    public ResponseModel updateOrders(Order order) {
        //判断是否存在
        String count = ordersMapper.countOrdersId(order.getId());
        String order1 = ordersMapper.getOrdersId(order.getId());
        if ("0".equals(count)){
            return ResponseHelper.buildResponseModel("主键不存在,修改失败");
        }else if(order1.equals(order.getOname())){
            return ResponseHelper.buildResponseModel("名称重复,修改失败");
        }
        ordersMapper.updateOrders(order);
        return ResponseHelper.buildResponseModel("修改成功");
    }

    @Override
    public ResponseModel updateOrdersPerson(OrdersPerson request) {
        //判断是否存在
        String count = ordersMapper.countOrdersId(request.getId());
        String order1 = ordersMapper.getOrdersId(request.getId());
        if ("0".equals(count)){
            return ResponseHelper.buildResponseModel("主键不存在,修改失败");
        }else if(order1.equals(request.getOname())){
            return ResponseHelper.buildResponseModel("名称重复,修改失败");
        }
        if(request.getPersons() != null){
            for (Personz personz: request.getPersons()) {
                personMapper.updatesPerson(personz);
            }
            personMapper.updateOrdersPerson(request);
            return ResponseHelper.buildResponseModel("修改成功");
        }else{
            personMapper.updateOrdersPerson(request);
            return ResponseHelper.buildResponseModel("id=" + request.getId() + "修改成功");
        }
    }
}
